source('code/analysis/load.R')

data = read_dta('data/working/defund-receiver.dta') %>%  
  mutate(exp = 'defund') %>% 
  bind_rows(read_dta('data/working/deport-receiver.dta') %>% mutate(exp = 'deport')) %>% 
  bind_rows(read_dta('data/working/defund-nocredible-receiver.dta') %>% mutate(exp = 'nocredible')) %>% 
  data_finalize() %>%
  mutate(deny = !bonus) %>%
  filter(age < 70 & age > 18) # overlap

# factor for raking
cols = c('age', 'black', 'asian', 'white', 'hisp', 'male', 'hs', 'bachelors')
data[cols] = lapply(data[cols], factor)  

# target sample
hc = data %>%
  filter(exp == 'defund') 
# main sample
lc = data %>%
  filter(exp == 'nocredible')  

# create the target weights for covariates
age_w= wpct(hc$age)
black_w= wpct(hc$black)
asian_w = wpct(hc$asian)
white_w = wpct(hc$white)
hisp_w = wpct(hc$hisp)
male_w = wpct(hc$male)
highschool_w = wpct(hc$hs)
bachelor_w = wpct(hc$bachelors)

targets = list(age_w, black_w, asian_w, white_w, hisp_w, male_w, highschool_w, bachelor_w)
names(targets) = c('age', 'black', 'asian', 'white', 'hisp', 'male', 'hs', 'bachelors')

lc$caseid = 1:length(lc$age)
lc = as.data.frame(lc)

anesrakefinder(targets, lc, choosemethod = "total")

# weighting process
match = anesrake(targets, lc, caseid = lc$caseid,
                  verbose= FALSE, choosemethod = "total",
                  iterate = TRUE , force1 = TRUE)

# create the weights
lc$weight = unlist(match[1])


lc$age = as.numeric(lc$age)

raking_results = function(data, measure) {

  label = 'defund-receiver-reweight'
  title = 'Auxiliary Experiment 6: lower-credibility variation, reweighted to match higher-credibility sample in Experiment 2 on demographics'
  depvar = ifelse(measure=='belief', 'Belief partner donated', 'Denied bonus to partner')
  
  if (measure == 'belief') {
    data$outcome = data$believe_donated
  } else {
    data$outcome = data$deny
  }

  model1 = lm(outcome~excuse, data=data, weights = weight)
  model2 = lm(outcome~excuse+age+agesq+race+hisp+male+education, data=data, weights = weight)
  mean_dv = formatC(mean(data$outcome[data$excuse==0]), digits=3, format='f')
  models = list(model1, model2)
  out = stargazer(models, 
                  omit='age|agesq|race|hisp|male|education|Constant',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq', 'n'), 
                  dep.var.labels = depvar,
                  title = title, 
                  label = str_interp('t:${label}'),
                  add.lines = list(c('Demographic controls', c('No','Yes'))))
  means = str_interp('No Cover mean & ${mean_dv} & ${mean_dv} \\\\')
  out = c(out[1:18], means, out[19:length(out)])
  out = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[[label]])
  return(out)
}



output1 = raking_results(data=lc, measure = 'belief')
output2 = raking_results(data=lc, measure = 'deny')


out = c(output1[1:9],
        '& \\multicolumn{2}{c}{Auxiliary Experiment 6} \\\\ \\cmidrule(lr){2-3}',
        output1[14:15],
        '\\textbf{Panel A:} & \\multicolumn{2}{c}{\\textit{Belief partner donated}} \\\\',
        '\\midrule',
        output1[16:18],
        output1[20], # mean
        output1[23], # R-squared
        '\\midrule',
        '\\textbf{Panel B:} & \\multicolumn{2}{c}{\\textit{Denied bonus to partner}} \\\\',
        '\\midrule',
        output2[16:18],
        output2[20], # mean
        output2[23], # R-squared
        '\\midrule',
        output2[21:22],
        output2[24:length(output2)])

write_lines(out, 'output/tables/defund-receiver-reweight.tex')


